package com.example.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Deposit;
import com.example.service.DepositService;
import com.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 存款类型管理控制器
 */
@Controller
@RequestMapping("/admin/deposit")
public class DepositController {

    @Autowired
    private DepositService depositService;

    /**
     * 存款类型管理页面
     */
    @GetMapping("/page")
    public String depositPage() {
        return "admin/deposit/index";
    }

    /**
     * 获取存款类型列表
     */
    @GetMapping("/list")
    @ResponseBody
    public Result<Page<Deposit>> list(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
            @RequestParam(value = "keyword", required = false) String keyword) {
        
        Page<Deposit> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<Deposit> queryWrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like(Deposit::getSavingName, keyword);
        }
        
        Page<Deposit> pageResult = depositService.page(pageParam, queryWrapper);
        return Result.success(pageResult);
    }

    /**
     * 添加存款类型
     */
    @PostMapping("/add")
    @ResponseBody
    public Result<?> add(
            @RequestParam("savingName") String savingName,
            @RequestParam(value = "savingDesc", required = false) String savingDesc) {
        
        Deposit deposit = new Deposit();
        deposit.setSavingName(savingName);
        deposit.setDescrip(savingDesc);
        
        boolean saved = depositService.save(deposit);
        if (saved) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    /**
     * 修改存款类型
     */
    @PostMapping("/update")
    @ResponseBody
    public Result<?> update(@RequestBody Deposit deposit) {
        boolean updated = depositService.updateById(deposit);
        if (updated) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    /**
     * 删除存款类型
     */
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Result<?> delete(@PathVariable("id") Integer id) {
        boolean removed = depositService.removeById(id);
        if (removed) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 获取存款类型信息
     */
    @GetMapping("/info/{id}")
    @ResponseBody
    public Result<Deposit> info(@PathVariable("id") Integer id) {
        Deposit deposit = depositService.getById(id);
        if (deposit != null) {
            return Result.success(deposit);
        } else {
            return Result.error("存款类型不存在");
        }
    }

    /**
     * 获取所有存款类型列表（不分页）
     */
    @GetMapping("/list-all")
    @ResponseBody
    public Result<List<Deposit>> listAll() {
        List<Deposit> list = depositService.list();
        return Result.success(list);
    }

    /**
     * 导出存款类型数据
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("存款类型信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        
        List<Deposit> list = depositService.list();
        EasyExcel.write(response.getOutputStream(), Deposit.class).sheet("存款类型信息").doWrite(list);
    }
} 